package com.xmkj.bearuser.home;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.banner.CBViewHolderCreator;
import com.common.banner.ConvenientBanner;
import com.common.banner.OnItemClickListener;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.HomeBean;
import com.common.retrofit.bean.SecondSortBean;
import com.common.retrofit.bearusermethod.HomeMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.BaseUrlURLActivity;
import com.xmkj.bearuser.R;
import com.xmkj.bearuser.count.LoginActivtiy;
import com.xmkj.bearuser.home.user.HomeMemberLikeAdapter;
import com.xmkj.bearuser.home.user.HomeNewAdapter;
import com.xmkj.bearuser.home.user.HomeRecommendAdapter;
import com.xmkj.bearuser.pk.GoodsDetailsActivity;
import com.xmkj.bearuser.sort.ThreeListActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Create by leo
 * 创建日期：2018/5/3
 * 描述：
 */

public class HomeFragment extends BaseMvpFragment implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.rl_search)
    RelativeLayout rlSearch;
    @BindView(R.id.home_view_pager)
    ConvenientBanner homeViewPager;
    @BindView(R.id.ll_shop_active)
    LinearLayout llShopActive;
    @BindView(R.id.ll_day_active)
    LinearLayout llDayActive;
    @BindView(R.id.ll_member_spicial)
    LinearLayout llMemberSpicial;
    @BindView(R.id.ll_pop_info)
    LinearLayout llPopInfo;
    @BindView(R.id.rl_recommend)
    RelativeLayout rlRecommend;
    @BindView(R.id.recyclerview_recommend)
    XRecyclerView recyclerviewRecommend;
    @BindView(R.id.rl_new)
    RelativeLayout rlNew;
    @BindView(R.id.recyclerview_new)
    XRecyclerView recyclerviewNew;
    @BindView(R.id.rl_most_like)
    RelativeLayout rlMostLike;
    @BindView(R.id.recyclerview_most_love)
    XRecyclerView recyclerviewMostLove;
    @BindView(R.id.swip_refresh)
    SwipeRefreshLayout swipRefresh;
    Unbinder unbinder;
    /*    @BindView(R.id.home_view_pager)
        ConvenientBanner homeViewPager;
        @BindView(R.id.iv_search)
        ImageView ivSearch;
        @BindView(R.id.iv_message)
        ImageView ivMessage;
        @BindView(R.id.iv_notice)
        ImageView ivNotice;
        @BindView(R.id.marqueeView)
        MarqueeView marqueeView;
        @BindView(R.id.swip_refresh)
        SwipeRefreshLayout swipRefresh;
        LinearLayout llSort;
        LinearLayout llShare;
        LinearLayout llChong;
        LinearLayout llNewHand;
        TabLayout tabLayout;
        ViewPager viewPager;*/
//    SparseArray<String> titles = new SparseArray<>();
    private List<String> viewPagerUrl = new ArrayList<>();
    private HomeBean homeBean;
    private int[] page_indicatorId;


    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void onViewCreated(View view) {

    }

    @Override
    protected void doLogicFunc() {
        swipRefresh.setOnRefreshListener(this);
        swipRefresh.setColorSchemeResources(R.color.main_color);
        attachClickListener(llDayActive);
        attachClickListener(llMemberSpicial);
        attachClickListener(llPopInfo);
        attachClickListener(llShopActive);
        attachClickListener(rlRecommend);
        attachClickListener(rlMostLike);
        attachClickListener(rlNew);
        getHeadDatas();
        attachClickListener(rlSearch);
    }

    private void getHeadDatas() {
        CommonSubscriber<HomeBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                swipRefresh.setRefreshing(false);
                dismissProgressDialog();
                homeBean = (HomeBean) o;

                setBanner();
                setRecommendRecyclerView();
                setNewRecyclerView();
                setMemberLikeRecyclerView();
//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                swipRefresh.setRefreshing(false);
                showToastMsg(e);
            }
        });
        uid = SPUtils.getShareInt(Constans.UID);
        hashid = SPUtils.getShareString(Constans.HASHID);
        HomeMethods.getInstance().appIndex(subscriber);
        rxManager.add(subscriber);
    }

    private void setMemberLikeRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerviewMostLove.setLayoutManager(layoutManager);
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerviewMostLove.setHasFixedSize(true);
        recyclerviewMostLove.setRefreshEnabled(false);
        recyclerviewMostLove.setLoadMoreEnabled(false);
        HomeMemberLikeAdapter adapter = new HomeMemberLikeAdapter(context, homeBean.viplike_list);
//        adapter.setOnItemClickSeclect(this);
        recyclerviewMostLove.setAdapter(adapter);
    }

    private void setNewRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerviewNew.setLayoutManager(layoutManager);
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerviewNew.setHasFixedSize(true);
        recyclerviewNew.setRefreshEnabled(false);
        recyclerviewNew.setLoadMoreEnabled(false);
        HomeNewAdapter adapter = new HomeNewAdapter(context, homeBean.new_list);
//        adapter.setOnItemClickSeclect(this);
        recyclerviewNew.setAdapter(adapter);
    }

    @Override
    protected void onViewClicked(View view) {
        if (EmptyUtils.isEmpty(SPUtils.getShareString(Constans.HASHID))) {
            gotoActivity(LoginActivtiy.class);
            return;
        }
        if (rlRecommend.getId() == view.getId()) {  // 1
            Intent intent = new Intent(context, HomeButtomThreeActivity.class);
            intent.putExtra(HomeButtomThreeActivity.TYPE, "1");
            context.startActivity(intent);

        } else if (rlMostLike.getId() == view.getId()) {  // 3
            Intent intent = new Intent(context, HomeButtomThreeActivity.class);
            intent.putExtra(HomeButtomThreeActivity.TYPE, "3");
            context.startActivity(intent);

        } else if (rlNew.getId() == view.getId()) {  // 2
            Intent intent = new Intent(context, HomeButtomThreeActivity.class);
            intent.putExtra(HomeButtomThreeActivity.TYPE, "2");
            context.startActivity(intent);
        } else if (llShopActive.getId() == view.getId()) { // 店铺活动
            Intent intent = new Intent(context, HomeTopFourActivity.class);
            intent.putExtra(HomeTopFourActivity.TYPE, "1");
            context.startActivity(intent);

        } else if (llDayActive.getId() == view.getId()) { // 节日活动
            Intent intent = new Intent(context, HomeTopFourActivity.class);
            intent.putExtra(HomeTopFourActivity.TYPE, "2");
            context.startActivity(intent);
        } else if (llMemberSpicial.getId() == view.getId()) { // :会员专区
            Intent intent = new Intent(context, HomeTopFourActivity.class);
            intent.putExtra(HomeTopFourActivity.TYPE, "3");
            context.startActivity(intent);
        } else if (llPopInfo.getId() == view.getId()) { // 时尚资讯
            Intent intent = new Intent(context, HomeTopFourActivity.class);
            intent.putExtra(HomeTopFourActivity.TYPE, "4");
            context.startActivity(intent);
        } else if (view.getId() == rlSearch.getId()) {
            gotoActivity(BearSearhActivity.class);
        }


    }


    private void setBanner() {
//        viewPagerUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525427891333&di=32991d497454624b5c490151a185a2c1&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0142135541fe180000019ae9b8cf86.jpg%401280w_1l_2o_100sh.png");
//        viewPagerUrl.add("http://img07.tooopen.com/images/20170316/tooopen_sy_201956178977.jpg");
//        viewPagerUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525427891333&di=32991d497454624b5c490151a185a2c1&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0142135541fe180000019ae9b8cf86.jpg%401280w_1l_2o_100sh.png");
        if (viewPagerUrl.size() == 0) {
            if (homeBean != null && homeBean.banner_list.size() > 0) {
                for (int i = 0; i < homeBean.banner_list.size(); i++) {
                    viewPagerUrl.add(homeBean.banner_list.get(i).pic);
                }
            }
        }

        page_indicatorId = new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused};
        homeViewPager.setPageIndicator(page_indicatorId);
        homeViewPager.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, viewPagerUrl);
        homeViewPager.setOnItemClickListener(this);
        homeViewPager.startTurning(5000);
    }


    @Override
    public void onItemClick(int position) {
        if (homeBean != null && homeBean.banner_list.size() > 0) {
            //分类
            if (EmptyUtils.isNotEmpty(homeBean.banner_list.get(position).c_id)) { // 分类id
                SecondSortBean.CategoryTwoEntity item = new SecondSortBean.CategoryTwoEntity();
                item.c_id = Integer.valueOf(homeBean.banner_list.get(position).c_id);
                item.c_name = "商品";
                Intent intent = new Intent(context, ThreeListActivity.class);
                intent.putExtra(ThreeListActivity.BEAN, item);
                startActivity(intent);
            }
            else {
                Intent intent = new Intent(context, GoodsDetailsActivity.class);
                intent.putExtra(GoodsDetailsActivity.ID,homeBean.banner_list.get(position).g_id);
                intent.putExtra(GoodsDetailsActivity.TYPE,"商品详情");
                startActivity(intent);
            }

      /*      Intent intent1 = new Intent(context, BaseUrlURLActivity.class);
            intent1.putExtra(BaseUrlURLActivity.MAIN_URL, homeBean.banner_list.get(position).url);
            intent1.putExtra(BaseUrlURLActivity.TITILE, "活动");
            context.startActivity(intent1);*/
        }
    }


    @Override
    public void onRefresh() {
        getHeadDatas();
    }


    @Override
    public void bindKnife() {
        unbinder = ButterKnife.bind(this, view);
    }

    private void setRecommendRecyclerView() {
//        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerviewRecommend.setLayoutManager(layoutManager);
//        recyclerview.addItemDecoration(new DividerGridItemDecoration(context));
      /*  recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));*/
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerviewRecommend.setHasFixedSize(true);
        recyclerviewRecommend.setRefreshEnabled(false);
        recyclerviewRecommend.setLoadMoreEnabled(false);
        HomeRecommendAdapter adapter = new HomeRecommendAdapter(context, homeBean.recommend_list);
//        adapter.setOnItemClickSeclect(this);
        recyclerviewRecommend.setAdapter(adapter);

    }


}
